package cn.thr.pethome.base.controller;

import cn.thr.pethome.base.query.BaseQuery;
import cn.thr.pethome.base.service.IBaseService;
import cn.thr.pethome.util.AjaxResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

public class BaseController<T> {

    @Autowired
    private IBaseService<T> iBaseService;

    @PostMapping("/list")
    @ResponseBody
    // @ApiOperation 标注这个方法是用来干嘛的！
    @ApiOperation(value = "这是获取所有部门的信息")
    public List<T> findAll(){
        return iBaseService.findAll();
    }


    //@GetMapping 查询一个发送get请求  "/{id}" 这个id只是一个占位符 等会这个id会传给Long id
    @GetMapping("/{id}")
    @ResponseBody
    @ApiOperation(value = "这个是用id来查询部门信息")
    //@PathVariable("id") 从path 路径中获取到的id 返回给Long id
    public AjaxResult findOne(@PathVariable("id") Long id){
        try {
            T one = iBaseService.findOne(id);
            return AjaxResult.success().setData(one).setMsg("查找到一坨狗屎");
            //通过工具类返回信息是否成功或者失败  setData(one)是用来返回数据 setMsg修改要显示的文字
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error().setMsg("来嘛，哦豁了嘛");
    }

    @DeleteMapping("/{id}")
    @ResponseBody
    @ApiOperation(value = "这是根据id删除数据")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            iBaseService.delete(id);
            return AjaxResult.success().setMsg("你猜下删除成功没有");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }

    }

    /**
     *
     * @RequestBody 是前端传进来
     * @ResponseBody 是后端返回给前端的
     *
     */
    @PostMapping
    @ResponseBody //前端传后端
    @ApiOperation(value = "添加")
    public AjaxResult add(@RequestBody T t){
        try {
                iBaseService.add(t);
            return AjaxResult.success().setMsg("你猜成功没得");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }

    }


    @PutMapping
    @ResponseBody //前端传后端
    @ApiOperation(value = "修改信息")
    public AjaxResult updaye(@RequestBody T t){
        try {
            iBaseService.update(t);
            return AjaxResult.success().setMsg("你猜成功没得");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }

    }


    /**
     *
     * @RequestBody 是前端传进来
     * @ResponseBody 是后端返回给前端的
     *
     */
    @PostMapping("/pageList")
    @ResponseBody
    @ApiOperation(value = "用来做分页查询的")
    public AjaxResult findQuery(@RequestBody BaseQuery query){
        try {
            //获取到当前页
            List<T> byQuery = iBaseService.findByQuery(query);
            //这是获取到总条数
            Long count = iBaseService.findByQueryCount(query);
            return AjaxResult.success().setData(byQuery).setTotal(count);
            //setData 用来返回数据的   setTotal 用来返回总条数
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    @PatchMapping("/del")
    @ResponseBody
    @ApiOperation(value = "这是用来批量删除的")
    //@RequestBody 把后端都数据返回给前端
    public  AjaxResult batchDel(@RequestBody List<Long> ids){
        try {
            iBaseService.batchDel(ids);
            return AjaxResult.success().setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error().setMsg("删除失败");
    }

}
